<script>
export default {
	globalData: {
		address: '',
		openId: null,
		token: null,
		location: {},//地址
		system: {},//系统配置
		userInfo: {},//用户信息.
		onAddress: {},
		onGoodCart: [],//购物车列表
		onCoupon: {},//当前优惠券
	},
	onLaunch: function () {
		// uni.setStorageSync('token','ab814477-8302-42d7-ad3a-a1f778d549d3')
		// let userInfo = uni.getStorageSync('userInfo')
		// let token = sessionStorage.getItem('token')
		// this.globalData.token = token
		// this.globalData.userInfo = userInfo

		// this.getSystem()
		// // #ifdef MP-WEIXIN
		// this.overShare()
		// // #endif
		// if (userInfo&&userInfo.id&&token) {
		// 	this.getUserInfo()
		// 	uni.switchTab({
		// 		url: '/pages/index/index'
		// 	})
		// } else {
		// }
	},
	onShow: function () {
		console.log('App Show')
		this.checkPagePermission();
		const token = sessionStorage.getItem('token');
		if (!token) {
			uni.showToast({
				title: '请重新登录',
				icon: 'none'
			});
			uni.reLaunch({
				url: '/'
			});
		}
	},
	onHide: function () {
		console.log('App Hide')
	},
	mounted() {
		// 监听浏览器关闭事件
		// window.addEventListener('beforeunload', this.deleteToken);
	},
	beforeDestroy() {
		// 移除事件监听器
		// window.removeEventListener('beforeunload', this.deleteToken);
	},
	methods: {
		checkPagePermission() {
			const currentPage = this.getCurrentPageUrl(); // 获取当前页面路径
			let routeList = [];
			if (uni.getStorageSync("flag") == "teacher") {
				routeList = [
					"#/pagesCopy/practiceTask/practice",
					"#/pagesCopy/practiceTask/practiceDetail",
					"#/pagesCopy/submitWork/weekWork",
					"#/pagesCopy/practiceTask/report",
					"#/pagesCopy/submitWork/weekWorkDetail",
					"#/pagesCopy/trivia/triviaTips",
					"#/pagesCopy/trivia/triviaAnswer",
					"#/pagesCopy/trivia/analyze",
				];

			} else if (uni.getStorageSync("flag") == "student") {
				routeList = [
					"#/teacher/practiceTask/practice",
					"#/teacher/practiceTask/addPractice",
					"#/teacher/examine/collegeAudit",
					"#/teacher/examine/schoolAudit",
					"#/teacher/practiceTask/practiceDetail",
					"#/teacher/trivia/index",
					"#/teacher/trivia/examinationPaperr",
					"#/teacher/trivia/taskDetail",
					"#/teacher/practiceTask/taskDetail",
				];
			}
			if (routeList.includes(currentPage.split("?")[0])) {
				setTimeout(() => {
					uni.showToast({
						title: '无权限访问',
						icon: 'none'
					});
					uni.reLaunch({ url: '/pages/index/index' }); // 跳转到登录页面
				}, 0);
			}
		},
		deleteToken() {
			// uni.removeStorageSync('sortedTabList');
			// uni.removeStorageSync('flag');
			// uni.removeStorageSync('getInfo');
			// uni.removeStorageSync('studentUnreport');
			// uni.removeStorageSync('token');
		},
		getCurrentPageUrl() {
			return window.location.hash; // 获取当前页面的 URL 路径
		},
		// #ifdef MP-WEIXIN
		overShare() {
			//监听路由切换
			wx.onAppRoute(function (res) {
				let pages = getCurrentPages(),
					view = pages[pages.length - 1]
				if (view) {
					wx.showShareMenu({
						withShareTicket: true,
						menus: ['shareAppMessage', 'shareTimeline']
					})
				}
			})
		},
		// #endif
		getSystem() {//获取系统配置
			this.axiosFromToken('POST', 'common/service', {}, '加载中').then(res => {
				this.globalData.system = res.data
				uni.setStorageSync('system', res.data)
				console.log(res, '系统配置')
			})
		},
		//获取用户信息
		getUserInfo() {
			this.axiosFromToken('POST', 'user/userinfo', {
				token: this.globalData.token
			}).then(res => {
				if (res.code == 1) {
					console.log('用户信息', res.data);
					this.globalData.userInfo = res.data
					uni.setStorageSync('userInfo', res.data)
				} else {
					this.$tools.showToast(res.msg)
				}
			})
		},
		getAddress() {//获取位置信息
			// #ifdef H5
			// //开发状态
			this.app.location.latitude = '36.667123'
			this.app.location.longitude = '117.076244'
			this.app.location.Address = '济南市'
			// #endif

			// #ifndef H5
			let that = this
			return new Promise((resolve, reject) => {
				if (that.globalData.location && that.globalData.location.Address && that.globalData.location.latitude && that.globalData.location.longitude) {
					resolve()
				} else {
					var myAmapFun = new that.$vm.$amapwx.AMapWX({ key: '32744ba7c2805fac5edc9f7bafc1e1ae' });
					uni.showLoading({
						title: '加载中',
					})
					myAmapFun.getRegeo({
						success: function (data) {
							uni.hideLoading()
							//获取用户所在地
							that.globalData.location = data[0]
							let address = data[0].regeocodeData.addressComponent
							if (typeof address.city == "string") {
								data[0].Address = address.city
							} else {
								data[0].Address = address.province
							}
							data[0].Address = data[0].Address.substring(0, data[0].Address.length - 1)
							// data[0].Address = '济南市'
							resolve(data[0].Address)
						},
						fail: function (info) {
							//失败回调
							uni.hideLoading()
							if (info.errMsg == "getLocation:fail auth deny") {
								that.showtt('请同意授权位置信息')
							}
							console.log(info)
						},

					})
				}
			})
			//  #endif
		},
		payFn(provider, parmas) {//拉起支付
			console.log(parmas, '支付参数')
			return new Promise((resolve, reject) => {
				uni.requestPayment({
					provider,
					// #ifdef MP-WEIXIN
					...parmas,
					// #endif
					// #ifndef MP-WEIXIN
					orderInfo: parmas,
					// #endif
					success: (res) => {
						uni.showToast({
							icon: 'success',
							title: '支付成功'
						})
						resolve(res)
					},
					fail: (err) => {
						uni.showToast({
							icon: 'error',
							title: '支付失败'
						})
						reject(err)
						// console.log('fail:', err, this);
					}
				});
			})
		}

	}
}
</script>

<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
/*每个页面公共css */
@import "/static/style/style_scss.scss";

page {
	background: #F7F7F7;
}
.uni-modal__hd {
	padding-top: 0.6em !important;
}
.uni-modal__title {
	font-size: 20px !important;
}
.uni-modal__textarea {
	padding: 8px !important;
	font-size: 16px !important;
}
.uni-modal__btn_default {
	background: #64606b;
	margin: 5px;
	border-radius: 25px;
	color: #fff !important;
}
.uni-modal__btn_primary {
	background: linear-gradient(rgb(72, 151, 255), rgb(72, 120, 255));;
	margin: 5px;
	border-radius: 25px;
	color: #fff !important;
}
.uni-modal__ft {
	width: 56%;
	margin: auto;
	margin-top: 5px;
	margin-bottom: 5px;
	line-height: 36px !important;
	font-size: 16px !important;
}
.uni-modal__bd {
	padding: 0.6em !important;
	font-size: 18px !important;
}
.uni-modal__ft:after {
	 display: none;
}
.uni-modal__btn:after {
	 display: none;
}

/* #ifdef MP-WEIXIN */
/* 自定义swpier指示器样式 */
wx-swiper .wx-swiper-dot {
	width: 12rpx;
	height: 6rpx;
	background: #fff;
	border-radius: 2rpx;
}

wx-swiper .wx-swiper-dot-active {
	width: 12rpx;
	height: 6rpx;
	background: #d4bc03;
	border-radius: 2rpx;
}

/* #endif */
</style>
